package org.ssc.audience.controller

import io.restassured.filter.session.SessionFilter
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock
import org.springframework.test.context.ActiveProfiles
import org.ssc.audience.AudienceApplication
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Timeout

import java.util.concurrent.TimeUnit

import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor
import static com.github.tomakehurst.wiremock.client.WireMock.moreThanOrExactly
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
import static com.github.tomakehurst.wiremock.client.WireMock.verify
import static io.restassured.RestAssured.given
import static java.util.concurrent.TimeUnit.SECONDS
import static org.hamcrest.number.OrderingComparison.lessThan

/**
 * Spock组件测试示例
 * Tickethouse使用WireMock模拟
 */
@SpringBootTest(classes = AudienceApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureWireMock(files = "classpath:mock", port = 9000)
@ActiveProfiles("test")
class AudienceHandlerComponentSpec extends Specification {

    @Shared
    def sessionFilter = new SessionFilter()

    /**
     * 在所有测试之前执行，用于用户登录
     * @return
     */
    def setupSpec() {
        given().
                baseUri("http://localhost:9001").
                param("aname", "aud1").
                param("password", "123456").
                filter(sessionFilter).
                when().
                post("/audience/login").
                then().
                statusCode(200)
    }


    @Timeout(value = 6000, unit = TimeUnit.MILLISECONDS)
    def "Get order by audience ID"() {
        expect: "Call getOrderByAid"
        given().
                baseUri("http://localhost:9001").
                filter(sessionFilter).
                get("/audience/getOrderByAid").
                then().
                statusCode(200).
                time(lessThan(6L), SECONDS)
        verify(moreThanOrExactly(1), getRequestedFor(urlEqualTo("/ticket/getOrderByAid/1")))
    }




}
